<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      size="small"
    >
      <!-- <el-form-item label="费用周期" prop="feeCycle">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.feeCycle"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择费用周期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="仪表编号" prop="meterNum">
        <el-select
          clearable
          v-model="queryParams.meterNum"
          placeholder="请选择仪表"
        >
          <el-option
            v-for="item in meterType"
            :key="item.id"
            :label="item.meterNum"
            :value="item.meterNum"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyMeterManagement:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyMeterManagement:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyMeterManagement:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyMeterManagement:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="nyMeterManagementList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="费用周期"
        align="center"
        prop="feeCycle"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feeCycle, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仪表类型" align="center" prop="meterTypeNew" />
      <el-table-column label="仪表编号" align="center" prop="meterNum" />
      <el-table-column label="安装位置" align="center" prop="installPosition" />
      <el-table-column label="实际用量" align="center" prop="actualDosage" />
      <el-table-column
        label="私表汇总用量"
        align="center"
        prop="summaryDosage"
      />
      <el-table-column label="新增分摊量" align="center" prop="addShare" />
      <el-table-column
        label="累计待分摊量"
        align="center"
        prop="cumulativeShare"
      />
      <el-table-column label="本次分摊量" align="center" prop="currentShare" />
      <el-table-column label="计费单价" align="center" prop="unitPrice" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyMeterManagement:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyMeterManagement:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仪管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="费用周期" prop="feeCycle">
          <el-date-picker
            clearable
            size="small"
            v-model="form.feeCycle"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择费用周期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="仪表" prop="meterId">
          <el-select clearable v-model="form.meterId" placeholder="请选择仪表">
            <el-option
              v-for="item in meterType"
              :key="item.id"
              :label="item.meterNum"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="实际用量" prop="actualDosage">
          <el-input v-model="form.actualDosage" placeholder="请输入实际用量" />
        </el-form-item>
        <el-form-item label="私表汇总用量" prop="summaryDosage">
          <el-input
            v-model="form.summaryDosage"
            placeholder="请输入私表汇总用量"
          />
        </el-form-item>
        <el-form-item label="新增分摊量" prop="addShare">
          <el-input v-model="form.addShare" placeholder="请输入新增分摊量" />
        </el-form-item>
        <el-form-item label="本次分摊量" prop="currentShare">
          <el-input
            v-model="form.currentShare"
            placeholder="请输入本次分摊量"
          />
        </el-form-item>
        <el-form-item label="累积待分摊量" prop="cumulativeShare">
          <el-input
            v-model="form.cumulativeShare"
            placeholder="请输入累积待分摊量"
          />
        </el-form-item>
        <el-form-item label="计费单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入计费单价" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNyMeterAccount,
 
} from "@/api/smartParks/energy/energyMeterSummaryTable";
import {
  listNyMeterManagement,
  getNyMeterManagement,
  addNyMeterManagement,
  updateNyMeterManagement,
  delNyMeterManagement
 
} from "@/api/smartParks/energy/energyMeterSummaryTableManagement";
import { listData } from "@/api/system/dict/data"; //字典接口

export default {
  name: "NyMeterManagement",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仪管理表格数据
      nyMeterManagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feeCycle: null,
        meterId: null,
        actualDosage: null,
        summaryDosage: null,
        addShare: null,
        cumulativeShare: null,
        currentShare: null,
        unitPrice: null,
        diffDosage: null,
        enterpriseId: null,
        roomId: null,
        unitId: null,
        buildingId: null,
        parkId: null,
        tenantId: null,
       category: 2,
      },
      meterType: [],
      // 表单参数
      workState: [],
      form: {},
      // 表单校验
      rules: {
        meterId: [
          {
            required: true,
            message: "仪表不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getListFirst();
    listNyMeterAccount({ category: 0 }).then((response) => {
      this.meterType = response.rows;
    });
  },
  methods: {
    getListFirst() {
      listData({ dictType: "meter_type" }).then((response) => {
        this.workState = response.rows;
      });
    },
    /** 查询仪管理列表 */
    async getList() {
      this.loading = true;
      await this.getListFirst();
      listNyMeterManagement(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          this.workState.forEach((itemType) => {
            if (itemType.dictValue === item.meterType) {
              item.meterTypeNew = itemType.dictLabel;
            }
          });
        });
        this.nyMeterManagementList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.nyMeterManagementList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        feeCycle: null,
        meterType: null,
        actualDosage: null,
        summaryDosage: null,
        addShare: null,
        cumulativeShare: null,
        currentShare: null,
        unitPrice: null,
        diffDosage: null,
        enterpriseId: null,
        roomId: null,
        unitId: null,
        buildingId: null,
        parkId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
       category: 2,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仪表管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getNyMeterManagement(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仪表管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNyMeterManagement(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNyMeterManagement(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除仪管理的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delNyMeterManagement(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "energy/nyMeterManagement/export",
        {
          ...this.queryParams,
        },
        `energy_nyMeterManagement.xlsx`
      );
    },
  },
};
</script>